import React, {
  Component,
  createRef,
  createContext,
  useRef,
  useState,
  useEffect,
} from "react";
import PropType from "prop-types";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import gsap from "gsap"; //动画库
import Stats from "stats.js"; //性能监控
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js"; // FBX 模型加载器
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"; //模型加载器
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader"; //模型解压缩

import "./Home.css";
import TagHook from "./components/tagHook";

import {loadShaderFromFile} from './utlis'

import { CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer.js"; //// 引入CSS2模型对象CSS2DObject
import { CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer.js"; // 引入CSS2渲染器CSS2DRenderer

import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer"; //EffectComposer 效果组合器 它是处理 WebGLRenderer 效果的 接收 renderer为参数
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass"; // RenderPass 处理通道 它是处理 scene 场景和相机 camera 的。 接收 scene camera 为参数
import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass"; //UnrealBloomPass 泛光通道
import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader.js"; //添加抗锯齿的效果
import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass"; // 高亮发光描边通道 OutlinePass(画布宽高二维向量，场景scene,相机camera)

import { SMAAPass } from "three/examples/jsm/postprocessing/SMAAPass.js"; // SMAA抗锯齿通道

import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass";
import { GammaCorrectionShader } from "three/examples/jsm/shaders/GammaCorrectionShader.js"; // 伽马校正后处理Shader

import WebGL from "three/examples/jsm//capabilities/WebGL.js";
import { ImprovedNoise } from "three/examples/jsm/math/ImprovedNoise"; //柏林噪声
// @ts-ignore
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min";


let vertex:any,fragment:any;


const asyncF = async () =>{
    // 初始化主色器程序
    vertex = await loadShaderFromFile("./shader/basic/circular.fs.glsl")
    fragment = await loadShaderFromFile("./shader/basic/circular.vt.glsl")
}

// var vertex = require('./shader/circular.fs.glsl');

// var fragment = require('./shader/circular.vt.glsl');

// import vertex from './shader/circular.fs.glsl';

// import fragment from './shader/circular.vt.glsl';

// import {vertex,fragment} from './shader/basic/demo'

if (WebGL.isWebGL2Available() === false) {
  document.body.appendChild(WebGL.getWebGL2ErrorMessage());
}

const width = window.innerWidth; //屏幕宽度
const height = window.innerHeight; //屏幕高度

// 渲染器
const renderer = new THREE.WebGLRenderer({
  antialias: true, //开启优化锯齿
});
renderer.setPixelRatio(window.devicePixelRatio); // 设置设备像素比。通常用于避免HiDPI设备上绘图模糊
renderer.setSize(width, height); // 将输出canvas的大小调整为(width, height)并考虑设备像素比，
// renderer.outputEncoding = THREE.sRGBEncoding; //定义渲染器的输出编码
// 相机
const camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  0.1,
  100
);
camera.position.set(0, 0, 1.5);
// 场景
const scene = new THREE.Scene();

// 轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// controls.target.set(0, 12, 0);

// 性能监测 (这个不是three必须项)
const stats = new Stats(); // 性能监测


const ReactDemo = () => {
  // 定义高斯函数

  useEffect(() => {
    animate();
    init();
  }, []);

  function init() {
    const container = document.createElement("div");
    document.body.appendChild(container);

    const axesHelper = new THREE.AxesHelper(90); // 辅助线
    scene.add(axesHelper);

    // 着色器材质(ShaderMaterial)
   const shaderMaterial = new THREE.ShaderMaterial({
    // 顶点着色器  （获取位置的点） void 表示没有返回值 main 入口函数 ，gl_Position 自带属性。可以获取到材质的 顶点数据（position） 注意结尾分好不能少
    //projectionMatrix * viewMatrix * modelMatrix * vec4(position,1.0);
    // 投影矩阵 * 视图矩阵 * 模型矩阵 * 顶点矩阵（顶点着色器，一直遵循改法则）
    vertexShader:vertex,

    //碎片着色器 （渲染颜色） void 表示没有返回值 main 入口函数,gl_FragColor 自带属性， vec4 四维向量 注意结尾分好不能少
    fragmentShader:fragment
   })
    //平面
    const floor = new THREE.Mesh(
      new THREE.PlaneGeometry( 1, 1 ),
      shaderMaterial
    )
   

    scene.add(floor)


    // 性能监测
    container.appendChild(stats.dom);
   
    container.appendChild(renderer.domElement);

    window.addEventListener("resize", onWindowResize);
    renderer.domElement.addEventListener("click", handleModelClick);
  }

  function onWindowResize() {
    camera.aspect = width / height;
    camera.updateProjectionMatrix();

    renderer.setSize(width, height);
  }

  function handleModelClick(event: MouseEvent) {
    const raycaster = new THREE.Raycaster();
    const pointer = new THREE.Vector2();
    pointer.x = (event.clientX / width) * 2 - 1;
    pointer.y = -(event.clientY / height) * 2 + 1;
    // 通过摄像机和鼠标位置更新射线
    raycaster.setFromCamera(pointer, camera);

    // 计算物体和射线的焦点
    const intersects: any = raycaster.intersectObjects(scene.children);

    console.log(intersects);
  }

  //

  function animate() {
    requestAnimationFrame(animate);

 

    renderer.render(scene, camera);
    controls.update();

    stats.update();
  }


  return <div></div>;
};

export default ReactDemo;
